//
//  RecipesVC.m
//  Hand-held_kitchen
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RecipesVC.h"
#import "JHCustomMenu.h"
#import "ZongTouwenjian.pch"
#import "SousuoVC.h"
#import "XinshourumenVC.h"
#import "ChangjingCaipuVc.h"
#import "CaiNiXiHuanVC.h"
#import "RIShiJi.h"
#import "ZhangchuXueYuanVC.h"
#import "ZhuyeCollCell.h"
#import "ZhuyeMoble.h"
#import "ZZYTAFetwork.h"
#import "ZhuyeWeb.h"
#import "ZhuyeTableMoble.h"
#import "ZhuyetableCell.h"
#import "HorViewController.h"
#import "JiaoCollCell.h"
#import "JiaoCollMoble.h"
@interface RecipesVC ()<JHCustomMenuDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,buttonDelegate,button2Delegate,button3Delegate>
@property (nonatomic, retain) JHCustomMenu *menu; // 导航
@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,retain)NSMutableArray *collectionData;
@property(nonatomic,retain)UIView *headView;
@property(nonatomic,retain)UIPageControl *page;/**<  页码控制器 */
@property(nonatomic,retain)UIScrollView *scroll;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *data;
@property(nonatomic,retain)NSMutableDictionary *dic;
@property(nonatomic,retain)NSString *str1;
@property(nonatomic,retain)NSString *str2;
@property(nonatomic,retain)NSString *str3;
@property(nonatomic,retain)NSMutableArray *jiaoData;
@property(nonatomic,retain)UICollectionView *jiaocollcteion;
@property(nonatomic,retain)UIView *jiaoView;
@property(nonatomic,assign)NSInteger i;
@end

@implementation RecipesVC
-(void)dealloc
{
    [_menu release];
    [_collection release];
    [_collectionData release];
    [_headView release];
    [_page release];
    [_scroll release];
    [_tableView release];
    [_data release];
    [_dic release];
    [_str1 release];
    [_str2 release];
    [_str3 release];
    [_jiaoData release];
    [_jiaocollcteion release];
    [_jiaoView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _i = 0;
    //导航不透明
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"主页";
    // view
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, 550* GAO)];
    
    // 左导航按钮
    [self daohang];
    // 右导航
    [self youdaohang];
    // collection数据
    [self data1];
    // coll
    [self collection1];
    /**<  页码控制器 */
    [self yema];
    [self scrollVie];
    /**<  图片 */
    [self img];
    /**<  tavledata */
    [self tableData];
    /**<  table */
    [self table];
    _tableView.tableHeaderView = _headView;
//    [self.view addSubview:_headView];
    self.jiaoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)];
    
    UILabel *caini = [[UILabel alloc]initWithFrame:CGRectMake(0, 5 * GAO, Width, 30 * GAO)];
    caini.font=[UIFont systemFontOfSize:14* KUAN];
    caini.text=@"猜你喜欢";
    caini.textAlignment = NSTextAlignmentCenter;
    [_jiaoView addSubview:caini];
    [caini release];
    [self shuaxin];
    
    
[self jiaoData1];
[self jiaocollctionView111];

     self.tableView.tableFooterView=_jiaoView;
    [self jiazai];
}

-(void)table
{
    self.tableView = [[UITableView alloc]initWithFrame:Frame style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[ZhuyetableCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}

-(void)tableData
{
    NSString *str = @"http://api.izhangchu.com/?methodName=SceneInfo&size=20&page=1&scene_id=114&version=4.3&";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.dic = responseObject[@"data"];
        self.data = _dic[@"dishes_list"];
        
//        self.data = [ZhuyeTableMoble modelHanderWithArray:arr];
        
        
        [_tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        
    ZhuyetableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.tupian sd_setImageWithURL:[NSURL URLWithString:_dic[@"scene_background"]]];
    cell.mingzi.text = _dic[@"scene_title"];
    cell.neirong.text =_dic[@"scene_title"];

    [cell.tupian1 sd_setImageWithURL:[NSURL URLWithString:_data[0][@"image"]]];
    [cell.tupian2 sd_setImageWithURL:[NSURL URLWithString:_data[1][@"image"]]];
    [cell.tupian3 sd_setImageWithURL:[NSURL URLWithString:_data[2][@"image"]]];
    [cell.tupian4 sd_setImageWithURL:[NSURL URLWithString:_data[3][@"image"]]];
    self.str1 =_data[0][@"material_video"];
    self.str2=_data[1][@"material_video"];
    self.str3 =_data[2][@"material_video"];
    
    
    cell.delegate = self;
    cell.delegate2 =self;
    cell.delegate3 =self;
    
    BLOCK push = ^(NSString *str)
    {
        HorViewController *dierye = [[HorViewController alloc]init];
        dierye.str = _data[3][@"material_video"];
        [self presentViewController:dierye animated:YES completion:^{
            
            
        }];
    } ;
    cell.button123456789 = push;
    return cell;
       
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210 * GAO;
}

-(void)button
{
    HorViewController *dierye = [[HorViewController alloc]init];
    dierye.str = _str1;
    [self presentViewController:dierye animated:YES completion:^{
    }];

}
-(void)button2
{
    HorViewController *dierye = [[HorViewController alloc]init];
    dierye.str = self.str2;
    [self presentViewController:dierye animated:YES completion:^{
    }];
    
}
-(void)button3
{
    HorViewController *dierye = [[HorViewController alloc]init];
    dierye.str =_str3;
    [self presentViewController:dierye animated:YES completion:^{
    }];
    
}


-(void)jiaoData1
{
    
    
    NSString *str = @"http://api.meishi.cc/v5/index3.php?format=json%20?lon=4.9E-324&source=android&page=3&lathttp://api.meishi.cc/v5/index3.php?format=json?lon=&source=android&lat=&c_md5=b79c4bb9d8b83017dcaea6fff3ca35c8&format=json&=4.9E-324&c_md5=eab2fbd9643207d85c970304896de5cb&format=json&";
    [ZZYTAFetwork postUrlString:str body:nil response:LMTJSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr = responseObject[@"obj"][@"customized"][@"data"];
        self.jiaoData =[ NSMutableArray array];
        self.jiaoData = [ JiaoCollMoble modelHanderWithArray:arr];
        
        
        [_jiaocollcteion reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
   
    
  
}


/**<  x上拉加载 */
-(void)jiazai
{
    MJRefreshAutoFooter *footer =[MJRefreshAutoFooter footerWithRefreshingBlock:^{
        NSString *str =[NSString stringWithFormat:@"http://api.meishi.cc/v5/index3.php?format=json?lon=4.9E-324&source=android&page=%ld&lathttp://api.meishi.cc/v5/index3.php?format=json?lon=&source=android&lat=&c_md5=b79c4bb9d8b83017dcaea6fff3ca35c8&format=json&=4.9E-324&c_md5=eab2fbd9643207d85c970304896de5cb&format=json&",_i];
        [ZZYTAFetwork  postUrlString:str body:nil response:LMTJSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSArray *arr = responseObject[@"obj"][@"customized"][@"data"];
            NSMutableArray *tepm = [JiaoCollMoble modelHanderWithArray:arr];
            for (JiaoCollMoble * m  in tepm) {
                [self.jiaoData addObject:m];
            }
            [_jiaocollcteion reloadData];
            
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            
        }];
        [_jiaocollcteion.mj_footer endRefreshing];
        
        
        
        
    }];
    _jiaocollcteion.mj_footer = footer;
}



#pragma mark - 下拉刷新
- (void)shuaxin
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),^{
                           
                           // 刷新
                           [self jiaoData1];
                           [self tableData];
                           [self data1];
                           //新版本是 MJ_header
                           [self.tableView.mj_header endRefreshing];
                       });
    }];
    //设置刷新控件
    self.tableView.mj_header = header;
    //设置文字
    header.arrowView.image = [UIImage imageNamed:@"jingxuan@3x"];
    //    [header setTitle:@"使劲拉!" forState:MJRefreshStateIdle];
    //    [header setTitle:@"松手刷!" forState:MJRefreshStatePulling];
    //    [header setTitle:@"等！！！" forState:MJRefreshStateRefreshing];
    header.stateLabel.font = [UIFont systemFontOfSize:12];
    
}



-(void)jiaocollctionView111
{
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.itemSize = CGSizeMake(111 * KUAN, 111 * GAO);
    // 行列间距
    layout1.minimumLineSpacing = 10 * KUAN;
    layout1.minimumInteritemSpacing = 10 * GAO;
    layout1.sectionInset = UIEdgeInsetsMake(10*GAO, 10*KUAN, 10*GAO, 10*KUAN);
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.jiaocollcteion = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 49*GAO, Width , Height) collectionViewLayout:layout1];
    _jiaocollcteion.backgroundColor = [UIColor whiteColor];
    _jiaocollcteion.dataSource =self;
    _jiaocollcteion.delegate =self;
    _jiaocollcteion.pagingEnabled = NO;
    _jiaocollcteion.contentInset = UIEdgeInsetsMake(0, 0, 100 * KUAN, 0);
    [_jiaocollcteion registerClass:[JiaoCollCell class] forCellWithReuseIdentifier:@"jiao"];
    [self.jiaoView addSubview:_jiaocollcteion];
    [_jiaocollcteion release];
    
    [layout1 release];
}




















-(void)img
{
    UIImageView *img6 = [[UIImageView alloc]initWithFrame:CGRectMake(20 * KUAN,520 * GAO, 30 * KUAN, 30* GAO)];
    img6.image = [UIImage imageNamed:@"6"];
    img6.backgroundColor = [UIColor redColor];
    [_headView addSubview:img6];
    UIImageView *img7 = [[UIImageView alloc]initWithFrame:CGRectMake(325 * KUAN,520*GAO, 30 * KUAN, 30  * GAO)];
    img7.image = [UIImage imageNamed:@"7"];
    img7.backgroundColor = [UIColor redColor];
    [_headView addSubview:img7];
}

-(void)scrollVie
{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 431 * GAO, Width, 80*GAO)];
    self.scroll.contentSize = CGSizeMake(Width * 5, 0);
    _scroll.backgroundColor = [UIColor yellowColor];
     self.scroll.pagingEnabled = YES;/**<  整页翻动 */
    _scroll.alwaysBounceHorizontal = YES; /**<  水平边缘弹动 */
    
    
    
    /**< bouces 边缘弹动 */
    _scroll.bounces = NO;
    /**< 水平(H)/垂直(V)边缘弹动 */
    // 允许在某一方向小于frame时 仍然有边缘弹动效果
//    _scroll.alwaysBounceHorizontal = NO;
    _scroll.delegate =self;

    UIImageView *img1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Width, 80* GAO)];
    img1.image = [UIImage imageNamed:@"11"];
    [_scroll addSubview:img1];
    
    UIImageView *img2 = [[UIImageView alloc]initWithFrame:CGRectMake(Width, 0 , Width, 80* GAO)];
    img2.image = [UIImage imageNamed:@"2"];
    [_scroll addSubview:img2];
    UIImageView *img3 = [[UIImageView alloc]initWithFrame:CGRectMake(Width *2, 0 , Width, 80 * GAO)];
    img3.image = [UIImage imageNamed:@"3"];
    [_scroll addSubview:img3];
    UIImageView *img4 = [[UIImageView alloc]initWithFrame:CGRectMake(Width *3,0, Width, 80 * GAO)];
    img4.image = [UIImage imageNamed:@"4"];
    [_scroll addSubview:img4];
    UIImageView *img5 = [[UIImageView alloc]initWithFrame:CGRectMake(Width *4, 0, Width, 80 *GAO)];
    img5.image = [UIImage imageNamed:@"5"];
    [_scroll addSubview:img5];
    
    [_headView addSubview:_scroll];
    [img1 release];
    [img2 release];
    [img3 release];
    [img4 release];
    [img5 release];
    [_scroll release];
}

/**<  页码控制器 */
-(void)yema
{
    /**< pageControl 页码控制器 */
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(140* KUAN, 510*GAO, 100* KUAN, 40*GAO)];
//    _page.backgroundColor = [UIColor blackColor];
    // 页数 保证控制器大小和页码数量一致
    _page.numberOfPages = 5;
    // 当前页
//    _page.currentPage = 1;
//    // 页码颜色
    _page.pageIndicatorTintColor = [UIColor blackColor];
//    // 当前页码颜色
    _page.currentPageIndicatorTintColor = [UIColor grayColor];
    // 添加事件
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    [_headView addSubview:_page];
    [_page release];
}

-(void)collection1
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(Width, 140*GAO);
    // 行列间距
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(10*GAO, 10* KUAN, 0, 10* KUAN);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Width, 431*GAO) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.dataSource =self;
    _collection.delegate =self;
    _collection.pagingEnabled = YES;
    [_collection registerClass:[ZhuyeCollCell class] forCellWithReuseIdentifier:@"cell"];
    [self.headView addSubview:_collection];
    

}

-(void)data1
{
    NSString *str = @"http://api.meishi.cc/v5/index3.php?format=json?lon=121.544678&source=android&lat=38.883338&c_md5=8d9b6770845c7e5e6bc1ee364fa6957c&format=json&";
    
    [ZZYTAFetwork postUrlString:str body:nil response:LMTJSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr = responseObject[@"obj"][@"san_can"];
        self.collectionData = [ZhuyeMoble modelHanderWithArray:arr];
      
        
        [_collection reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
   
}






- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_collection == collectionView) {

        return _collectionData.count;
    }else
    {
        return _jiaoData.count;
    }
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _collection) {
        
        ZhuyeCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        ZhuyeMoble *m = _collectionData[indexPath.item];
        [cell.tupian sd_setImageWithURL:[NSURL URLWithString:m.titlepic]];
        //    if (indexPath.item >=5) {
        cell.mingzi.text = m.title;
        cell.jianjie.text = m.descr;
        //    }else
        //    {
        //        cell.mingzi1.text = m.title;
        //        cell.jianjie1.text = m.descr;
        //    }
        
        return cell;
    }  else {
        
        JiaoCollCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"jiao" forIndexPath:indexPath];
        JiaoCollMoble *m1 = _jiaoData[indexPath.item];
        [cell1.tupian sd_setImageWithURL:[NSURL URLWithString:m1.titlepic]];
//        cell1.zi.text = m1.title;
        
        return cell1;
    }
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collection == collectionView) {
        ZhuyeWeb *dieye = [[ZhuyeWeb alloc]init];
        ZhuyeMoble *m = _collectionData[indexPath.item];
        dieye.str  = m.id1;
        [self.navigationController pushViewController:dieye animated:YES];
    }else{
        ZhuyeWeb *dieye = [[ZhuyeWeb alloc]init];
        JiaoCollMoble *m = _jiaoData[indexPath.item];
        dieye.str  = m.id1;
        [self.navigationController pushViewController:dieye animated:YES];
        
    }
    
    
}

// 点跟随图片变化
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
    _page.currentPage = _scroll.contentOffset.x / Width;
    
    _page.currentPage = _collection.contentOffset.x / Width;
    
    if (scrollView.contentOffset.x > Width * 4) {
        scrollView.contentOffset = CGPointMake(Width * 4, 0);
    }
    
    if (scrollView == _scroll) {
        [_collection setContentOffset:_scroll.contentOffset animated:YES];
    }
    if (scrollView == _collection) {
        
        [_scroll setContentOffset:_collection.contentOffset animated:YES];
    }
    if (scrollView == _jiaocollcteion) {
        [_tableView setContentOffset:_collection.contentOffset animated:YES];
    }
    
    
    
 
}
// pageAction
- (void)pageAction:(UIPageControl *)page
{
    [_scroll setContentOffset:CGPointMake(page.currentPage * Width, 0) animated:YES];
    [_collection setContentOffset:CGPointMake(page.currentPage * Width, 0) animated:YES];
}




//右导航按钮
-(void)youdaohang
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:(UIBarButtonItemStylePlain) target:self action:@selector(youdaohang:)];
    self.navigationItem.rightBarButtonItem  =rightItem;
}


- (void)youdaohang:(UIButton *)button
{
    SousuoVC *sousuo = [[SousuoVC alloc]init];
    [self.navigationController pushViewController:sousuo animated:YES];
//    [self presentViewController:sousuo animated:YES completion:^{
//        
//        
//    }];
}

//左导航按扭
-(void)daohang
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"schoolListItem.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showSchoolList:)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
}

// 左按钮方法
- (void)showSchoolList:(UIBarButtonItem *)barButtonItem
{
    __block __typeof(self) weakSelf = self;
    if (!self.menu) {
        self.menu = [[JHCustomMenu alloc] initWithDataArr:@[@"新手入门", @"场景菜谱", @"猜你喜欢", @"日食记", @"掌厨学院"] origin:CGPointMake(0, 0) width:125 rowHeight:44];
        _menu.delegate = self;
        _menu.dismiss = ^() {
            weakSelf.menu = nil;
        };
        _menu.arrImgName = @[@"item_school.png", @"item_battle.png", @"item_list.png", @"item_chat.png", @"item_share.png"];
        [self.view addSubview:_menu];
    } else {
        [_menu dismissWithCompletion:^(JHCustomMenu *object) {
            weakSelf.menu = nil;
        }];
    }
}
// 左按钮点击方法
- (void)jhCustomMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        XinshourumenVC *dierye = [[XinshourumenVC alloc]init];
        [self.navigationController pushViewController:dierye animated:YES];
        [dierye release];
    }else if(indexPath.row == 1){
        ChangjingCaipuVc *dierye = [[ChangjingCaipuVc alloc]init];
//         self.hidesBottomBarWhenPushed = YES;
//  [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController pushViewController:dierye animated:YES];
        [dierye release];
    }else if (indexPath.row ==2)
    {
        CaiNiXiHuanVC *dierye= [[CaiNiXiHuanVC alloc]init];
        [self.navigationController pushViewController:dierye animated:YES];
    }else if(indexPath.row == 3)
    {
        RIShiJi *dierye = [[RIShiJi alloc]init];
        [self.navigationController pushViewController:dierye animated:YES];
    }else{
        ZhangchuXueYuanVC *dierye = [[ZhangchuXueYuanVC alloc]init];
        [self.navigationController pushViewController:dierye animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
